package br.com.minhasfinancas.api.model;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;

import br.com.minhasfinancas.fw.model.AbstractModel;

@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = "login", name = "UN_USUARIO_LOGIN"))
@NamedQueries({
		@NamedQuery(name = "Usuario.findAll", query = "SELECT u FROM Usuario u"),
		@NamedQuery(name = "Usuario.findById", query = "SELECT u FROM Usuario u WHERE u.id = :id"),
		@NamedQuery(name = "Usuario.findByEmail", query = "SELECT u FROM Usuario u WHERE u.email = :email"),
		@NamedQuery(name = "Usuario.findByLogin", query = "SELECT u FROM Usuario u WHERE u.login = :login"),
		@NamedQuery(name = "Usuario.findByIdEager", query = "SELECT u FROM Usuario u LEFT JOIN FETCH u.contas WHERE u.id = :id")})
public class Usuario extends AbstractModel<Long> {
	private static final long serialVersionUID = 1L;
	
	public static final String FINDALL = "Usuario.findAll";
	public static final String FINDBYID = "Usuario.findByid";
	public static final String FINDBYEMAIL = "Usuario.findByEmail";
	public static final String FINDBYLOGIN = "Usuario.findByLogin";
	public static final String FINDBYIDEAGER = "Usuario.findByIdEager";

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "ID_USUARIO")
	private Long id;

	@NotNull
	@Column(name = "NOME", length = 25)
	private String nome;

	@NotNull
	@Column(name = "EMAIL", length = 45)
	private String email;

	@NotNull
	@Column(name = "LOGIN", length = 25)
	private String login;

	@NotNull
	@Column(name = "SENHA")
	private String senha;

	@OneToMany(mappedBy = "usuario", fetch = FetchType.LAZY)
	private Set<Conta> contas;

	public Usuario() {

	}

	@Override
	public Long getId() {
		return this.id;
	}

	@Override
	public void setId(Long id) {
		this.id = id;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getSenha() {
		return senha;
	}

	public void setSenha(String senha) {
		this.senha = senha;
	}

	public Set<Conta> getContas() {
		return contas;
	}

	public void setContas(Set<Conta> contas) {
		this.contas = contas;
	}

}
